﻿using System.Collections.Generic;
using System.Data;
using Moq;
using Nvigorate.Data;

namespace Nvigorate.Test.Data
{
    public static class Extension
    {
        /// <summary>
        /// Create an expectation taht GetParameter will be called for each parameter
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="parameters"></param>
        /// <param name="testBase"></param>
        public static void ExpectGetParameters(this Mock<IDatabaseProvider> provider,
                                               Dictionary<string, object> parameters, DataTestBase testBase)
        {
            foreach (var pair in parameters)
            {
                var item = pair;
                provider.Expect(p => p.GetParameter(item.Key, It.IsAny<object>(), It.IsAny<ParameterDirection>())).
                    Returns(
                    testBase.CreateParameter(item.Key, item.Value).Object);
            }
        }

        /// <summary>
        /// Create expectation that the specified command will be created if any GetCommand request
        /// with the commandText is encountered.
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="commandText"></param>
        /// <param name="provider"></param>
        /// <param name="command"></param>
        public static void ExpectGetCommand(this Mock<IDatabaseCommandFactory> factory,
                                            string commandText, IDatabaseProvider provider,
                                            IDatabaseCommand command)
        {
            factory.Expect(f => f.GetCommand(commandText, provider, It.IsAny<int>(),
                                             It.IsAny<IEnumerable<IDbDataParameter>>())).Returns(command);
            factory.Expect(f => f.GetCommand(commandText, provider)).Returns(command);
            factory.Expect(f => f.GetCommand(It.Is<IDbCommand>(c => c.CommandText == commandText),
                                             It.IsAny<IDatabaseProvider>())).Returns(command);
            factory.Expect(f => f.GetCommand(commandText,
                                             It.IsAny<IDatabaseProvider>(), It.IsAny<int>(),
                                             It.IsAny<DataRow[]>())).Returns(command);
        }
    }
}